// pages/order-list/index.js
var url = getApp().globalData.ApiUrl;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: [
      {
        order_state: '',
        name: "全部",
        status: ''
      },
      {
        order_state: 10,
        name: "待支付",
        status: "state_new"
      }, {
        order_state: 30,
        name: "待收货",
        status: "state_send"
      }, {
        order_state: 40,
        name: "已完成",
        status: "state_noeval"
      }
    ],
    current: 2,
    order_list: [],   // 订单列表
    goods: [],      // 商品列表
    show: false,
    order_key: "",
    searchValue: '',
    list: [],
    state: "",
    key: wx.getStorageSync('key'),
    request_url: getApp().globalData.ApiUrl,
    pay_sn: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var _this = this;
    this.getOrderList("state_send");
  },
  // 订单状态
  changeState: function (options) {
    this.setData({
      current: options.currentTarget.dataset.index,
      state: options.currentTarget.dataset.order_state
    })
    this.getOrderList(options.currentTarget.dataset.status)
  },
  // 订单搜索
  searchTap: function (e) {
    this.setData({
      searchValue: e.detail.value,
    })
    this.getOrderList()
  },
  // 获取订单列表
  getOrderList: function (options) {
    console.log("foshfjosh")
    var key = wx.getStorageSync("key")
    wx.showLoading({
      title: '加载中...',
    })
    var data = {
      key: key,
    }
    if (options) {
      data.state_type = options;
    };
    if (this.data.searchValue) {
      data.order_key = this.data.searchValue;
    }
    wx.request({
      url: url + '/mobile/index.php?act=member_order&op=order_list',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      data: data,
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 200) {
          var arr = [];
          var arr1 = [];
          var list = res.data.datas.order_group_list;
          for (var i in list) {
            var order_list = list[i].order_list;
            for (var order_key in order_list) {
              arr.push(order_list[order_key]);
            }
          }
          if (list.length == 0) {
            this.setData({
              order_list: [],
              show: true
            })
          } else {
            this.setData({
              order_list: arr,
              show: false
            })
          }
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
          this.setData({
            order_list: [],
            show: true
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
        })
      }
    })
  },
  toStore: (e) => {
    wx.navigateTo({
      url: '/pages/store/home/index?store_id=' + e.currentTarget.dataset.store_id,
    })
  },
  // 跳转订单详情
  toOrderDetail: (e) => {
    var order_id = e.currentTarget.dataset.order_id;
    var pay_sn = e.currentTarget.dataset.pay_sn;
    var order_type = e.currentTarget.dataset.order_type;   // 订单类型
    var log_id = e.currentTarget.dataset.log_id;
    var buyer_id = e.currentTarget.dataset.buyer_id;
    var state_desc = e.currentTarget.dataset.state_desc;
    if (state_desc == '待拼团') {
      wx.navigateTo({
        url: '/pages/my-group/index?order_id=' + order_id + '&pay_sn=' + pay_sn + '&log_id=' + log_id + '&buyer_id=' + buyer_id,
      })
    } else if (state_desc == '待砍价') {
      wx.navigateTo({
        url: '/pages/my-bargain/index?order_id=' + order_id + '&pay_sn=' + pay_sn + '&log_id=' + log_id + '&buyer_id=' + buyer_id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/order/order-detail/index?order_id=' + order_id + '&pay_sn=' + pay_sn,
      })
    }
  },
  // 物流
  toWuLiu: function (e) {
    var ids = e.currentTarget.dataset.order_id;
    var img = e.currentTarget.dataset.goods_img;
    var desc = e.currentTarget.dataset.state_desc;
    var num = e.currentTarget.dataset.goods_num;
    wx.navigateTo({
      url: '/pages/order/order-logistics/index?order_id=' + ids + '&goods_image_url=' + img + '&state_desc=' + desc + '&goods_num=' + num,
    })
  },
  // 去付款
  submitForm: function (e) {
    var key = wx.getStorageSync("key");
    var pay_sn = e.currentTarget.dataset.pay_sn;
    wx.request({
      url: url + '/mobile/index.php?act=member_payment&op=applet_pay',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      dataType: 'json',
      data: {
        key: key,
        pay_sn: pay_sn
      },
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.datas.timeStamp,
          'nonceStr': res.data.datas.nonceStr,
          'package': res.data.datas.package,
          'signType': 'MD5',
          'paySign': res.data.datas.paySign,
          'success': function (res) {
            if (status == 2) {
              wx.navigateTo({
                url: '/pages/my-group/index?buyer_id=' + that.data.buyer_id + '&pay_sn=' + that.data.pay_sn,
              });
            }
            if (status == 3) {
            }
            if (status == 1) {
              console.log('这是个普通订单');
            }
            wx.switchTab({
              url: '/pages/my/index',
            })
          },
          'fail': function (res) {
            if (res.errMsg == 'requestPayment:fail cancel') {
              wx.showToast({
                title: '取消付款',
                icon: 'none',
                duration: 800
              })
            }
          }
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
})